var UserTable = {
    id: "userTable",//表格id
    seItem: null,		//选中的条目
    me : ""
};


/**
 * 初始化整个表格
 */
UserTable.init = function(){
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(UserTable.id,Feng.ctxPath+"/sysUser/allParam",defaultColunms);
    bstable.setData({});
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
UserTable.initColumn = function(){
    var columns = [
        {title: '序号',align: 'center',valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index+1;
            }},
        {title: '用户名',field: 'username',align: 'center',valign: 'middle'},
        {title: '姓名',field: 'truename',align: 'center',valign: 'middle'},
        {title: '手机号码',field: 'phone',align: 'center',valign: 'middle'},
        {title: '账户类型',field: 'type',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
            if(value === "SYSTEM_ADMIN"){
                return "超级管理员";
            }else if(value === "COMPANY_ADMIN"){
                return "企业管理员";
            }
        }},
        {title: '创建时间',field: 'createDate',align: 'center',valign: 'middle'},
        {title: '最后更新时间',field: 'updateDate',align: 'center',valign: 'middle'},
        {title: '状态',field: 'state',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                if(value === "1"){
                    return "正常";
                }else if(value === "2"){
                    return "锁定";
                }
            }},
        {title: '操作',field: '#',align: 'center',formatter:operateFormatter,events:{
                'click .edit': function (e, value, row, index) {
                    editBtn(row.id);
                } ,
                'click .delete':function(e,value,row,index){
                    deleteBtn(row.id,row.username);
                }
        }
        }];
    return columns;
};

/**
 * 格式化列表中的事件
 */
function operateFormatter(){
    var operateDiv = $("#operateDiv").html();
    return operateDiv;
}


/**
 * 点击搜索按钮
 * @returns
 */
function clickSearch(){
    var queryData = {};
    var val_search_username = $("#txt_search_username").val();
    if (val_search_username !== "" || val_search_username !== null) {
        queryData['username'] =  val_search_username;
    }
    var val_search_truename = $("#txt_search_truename").val();

    if (val_search_truename !== "" || val_search_truename !== null) {
        queryData['truename'] = val_search_truename;
    }
    UserTable.me.destroy(queryData);
    // UserTable.me.refresh({query: queryData});
}

/**
 * 点击添加按钮时，跳出添加用户对话框
 * @returns
 */
function clickAddBtn(){
    var index = layer.open({
        type: 1,
        title: '添加用户',
        skin: 'layui-layer-lan', //加上边框
        area: [UserInfoDlg.width, "auto"],//宽高
        content: UserInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            UserInfoDlg.renderAdd(index);

        },
        maxmin: true,
        scrollbar: false,
        btn:['保存','关闭'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            UserInfoDlg.addSubmit(index);
        },btn2:function (index) {
            layer.close(index);
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    UserInfoDlg.currentLayerIndex = index;
}

/**
 * 点击编辑按钮时，跳出用户详情对话框
 * @returns
 */
function editBtn(uid){
    var index = layer.open({
        type: 1,
        title: '编辑用户',
        skin: 'layui-layer-lan',
        area: [UserInfoDlg.width,'auto'],
        content: UserInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            UserInfoDlg.renderAdd();
            UserInfoDlg.load(uid);
        },
        maxmin:true,
        scrollbar: false,
        btn:['保存','关闭'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            UserInfoDlg.editSubmit();
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },btn2:function (index) {
            layer.close(index);
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    UserInfoDlg.currentLayerIndex = index;
}

/**
 * 删除某个用户
 * @param uid 用户的id
 * @returns
 */
function deleteBtn(uid,deleteName){
    layer.confirm('是否删除用户:'+deleteName, {icon: 10, title:'是否删除用户?'}, function(index){
        var ajax = new $ax(Feng.ctxPath + "/sysUser/deleteById", function(data){
            if (data.success){
                Feng.success("删除成功!");
                UserTable.me.refresh();
                layer.close(index);
            }else if(data.code === 2){
                Feng.fail(data.msg);
            }else{
                Feng.fail("删除失败!");
            }
        },function(data){
            Feng.error("删除失败!");
        });
        ajax.data={id:uid};
        ajax.start();
    });
}


$(function(){
    //初始化用户管理表格
    UserTable.init();

    UserInfoDlg.init();
});
